From 26453d70cc99265e8ffaac2bce497fcc3b24dbb5 Mon Sep 17 00:00:00 2001 From: "chris@kneesaa.uk.xensource.com" Date: Wed, 9 Aug 2006 15:03:38 +0100 Subject: [PATCH] [qemu] Add vncunused option. If the port used for the requested display number is in use, try additional ports until a free port is found. Signed-off-by: Christian Limpach --- tools/examples/xmexample.hvm | 4 ++++ tools/ioemu/vl.c | 12 +++++++++++- tools/ioemu/vl.h | 2 +- tools/ioemu/vnc.c | 17 ++++++++++++----- tools/python/xen/xend/image.py | 3 +++ tools/python/xen/xm/create.py | 7 ++++++- 6 files changed, 37 insertions(+), 8 deletions(-) diff --git a/tools/examples/xmexample.hvm b/tools/examples/xmexample.hvm index 3d7f85263f..396274c860 100644 --- a/tools/examples/xmexample.hvm +++ b/tools/examples/xmexample.hvm @@ -129,6 +129,10 @@ vnc=1 # set VNC display number, default = domid #vncdisplay=1 +#---------------------------------------------------------------------------- +# try to find an unused port for the VNC server, default = 1 +#vncunused=1 + #---------------------------------------------------------------------------- # enable spawning vncviewer for domain's console # (only valid when vnc=1), default = 0 diff --git a/tools/ioemu/vl.c b/tools/ioemu/vl.c index 423c385e3a..76c4c76fc6 100644 --- a/tools/ioemu/vl.c +++ b/tools/ioemu/vl.c @@ -121,6 +121,7 @@ int bios_size; static DisplayState display_state; int nographic; int vncviewer; +int vncunused; const char* keyboard_layout = NULL; int64_t ticks_per_sec; int boot_device = 'c'; @@ -5344,6 +5345,7 @@ void help(void) "-loadvm file start right away with a saved state (loadvm in monitor)\n" "-vnc display start a VNC server on display\n" "-vncviewer start a vncviewer process for this domain\n" + "-vncunused bind the VNC server to an unused port\n" "-timeoffset time offset (in seconds) from local time\n" "-acpi disable or enable ACPI of HVM domain \n" "\n" @@ -5435,6 +5437,7 @@ enum { QEMU_OPTION_timeoffset, QEMU_OPTION_acpi, QEMU_OPTION_vncviewer, + QEMU_OPTION_vncunused, }; typedef struct QEMUOption { @@ -5512,6 +5515,7 @@ const QEMUOption qemu_options[] = { { "smp", HAS_ARG, QEMU_OPTION_smp }, { "vnc", HAS_ARG, QEMU_OPTION_vnc }, { "vncviewer", 0, QEMU_OPTION_vncviewer }, + { "vncunused", 0, QEMU_OPTION_vncunused }, /* temporary options */ { "usb", 0, QEMU_OPTION_usb }, @@ -5888,6 +5892,7 @@ int main(int argc, char **argv) snapshot = 0; nographic = 0; vncviewer = 0; + vncunused = 0; kernel_filename = NULL; kernel_cmdline = ""; #ifndef CONFIG_DM @@ -6295,6 +6300,11 @@ int main(int argc, char **argv) case QEMU_OPTION_vncviewer: vncviewer++; break; + case QEMU_OPTION_vncunused: + vncunused++; + if (vnc_display == -1) + vnc_display = -2; + break; } } } @@ -6504,7 +6514,7 @@ int main(int argc, char **argv) if (nographic) { dumb_display_init(ds); } else if (vnc_display != -1) { - vnc_display_init(ds, vnc_display); + vnc_display = vnc_display_init(ds, vnc_display, vncunused); if (vncviewer) vnc_start_viewer(vnc_display); xenstore_write_vncport(vnc_display); diff --git a/tools/ioemu/vl.h b/tools/ioemu/vl.h index a95d0687a5..7ed39bfcf6 100644 --- a/tools/ioemu/vl.h +++ b/tools/ioemu/vl.h @@ -784,7 +784,7 @@ void sdl_display_init(DisplayState *ds, int full_screen); void cocoa_display_init(DisplayState *ds, int full_screen); /* vnc.c */ -void vnc_display_init(DisplayState *ds, int display); +int vnc_display_init(DisplayState *ds, int display, int find_unused); int vnc_start_viewer(int port); /* ide.c */ diff --git a/tools/ioemu/vnc.c b/tools/ioemu/vnc.c index 3c0a7cdad9..129492f5e5 100644 --- a/tools/ioemu/vnc.c +++ b/tools/ioemu/vnc.c @@ -1183,7 +1183,7 @@ static void vnc_listen_read(void *opaque) } } -void vnc_display_init(DisplayState *ds, int display) +int vnc_display_init(DisplayState *ds, int display, int find_unused) { struct sockaddr_in addr; int reuse_addr, ret; @@ -1214,10 +1214,6 @@ void vnc_display_init(DisplayState *ds, int display) exit(1); } - addr.sin_family = AF_INET; - addr.sin_port = htons(5900 + display); - memset(&addr.sin_addr, 0, sizeof(addr.sin_addr)); - reuse_addr = 1; ret = setsockopt(vs->lsock, SOL_SOCKET, SO_REUSEADDR, (const char *)&reuse_addr, sizeof(reuse_addr)); @@ -1226,7 +1222,16 @@ void vnc_display_init(DisplayState *ds, int display) exit(1); } + retry: + addr.sin_family = AF_INET; + addr.sin_port = htons(5900 + display); + memset(&addr.sin_addr, 0, sizeof(addr.sin_addr)); + if (bind(vs->lsock, (struct sockaddr *)&addr, sizeof(addr)) == -1) { + if (find_unused && errno == EADDRINUSE) { + display++; + goto retry; + } fprintf(stderr, "bind() failed\n"); exit(1); } @@ -1247,6 +1252,8 @@ void vnc_display_init(DisplayState *ds, int display) vs->ds->dpy_refresh = vnc_dpy_refresh; vnc_dpy_resize(vs->ds, 640, 400); + + return display; } int vnc_start_viewer(int port) diff --git a/tools/python/xen/xend/image.py b/tools/python/xen/xend/image.py index 38547a8856..93f1c9f810 100644 --- a/tools/python/xen/xend/image.py +++ b/tools/python/xen/xend/image.py @@ -307,6 +307,7 @@ class HVMImageHandler(ImageHandler): vnc = sxp.child_value(config, 'vnc') vncdisplay = sxp.child_value(config, 'vncdisplay', int(self.vm.getDomid())) + vncunused = sxp.child_value(config, 'vncunused') sdl = sxp.child_value(config, 'sdl') ret = [] nographic = sxp.child_value(config, 'nographic') @@ -315,6 +316,8 @@ class HVMImageHandler(ImageHandler): return ret if vnc: ret = ret + ['-vnc', '%d' % vncdisplay, '-k', 'en-us'] + if vncunused: + ret += ['-vncunused'] return ret def createDeviceModel(self): diff --git a/tools/python/xen/xm/create.py b/tools/python/xen/xm/create.py index 545d127595..549018e209 100644 --- a/tools/python/xen/xm/create.py +++ b/tools/python/xen/xm/create.py @@ -412,6 +412,11 @@ gopts.var('vncdisplay', val='', fn=set_value, default=None, use="""VNC display to use""") +gopts.var('vncunused', val='', + fn=set_bool, default=1, + use="""Try to find an unused port for the VNC server. + Only valid when vnc=1.""") + gopts.var('sdl', val='', fn=set_value, default=None, use="""Should the device model use SDL?""") @@ -627,7 +632,7 @@ def configure_hvm(config_image, vals): """ args = [ 'device_model', 'pae', 'vcpus', 'boot', 'fda', 'fdb', 'localtime', 'serial', 'stdvga', 'isa', 'nographic', 'soundhw', - 'vnc', 'vncdisplay', 'vncconsole', 'sdl', 'display', + 'vnc', 'vncdisplay', 'vncunused', 'vncconsole', 'sdl', 'display', 'acpi', 'apic', 'xauthority', 'usb', 'usbdevice' ] for a in args: if (vals.__dict__[a]): -- 2.30.2